package com.jydd.singlekotlinmvvm.comm.feature

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
import com.bumptech.glide.Glide
import com.jydd.singlekotlinmvvm.R
import kotlinx.android.synthetic.main.activity_main.*
import org.koin.android.viewmodel.ext.android.viewModel

class MainActivity : AppCompatActivity() {
    private lateinit var navController : NavController
    private val launchViewModel by viewModel(LauncherViewModel::class)
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        window.statusBarColor = Color.TRANSPARENT
        launchViewModel.apply {
                userInfo.observe(this@MainActivity, androidx.lifecycle.Observer {
                        it?.let {
                            navMain.getHeaderView(0).findViewById<AppCompatTextView>(R.id.tvNickname).text = it.nickName
                            navMain.getHeaderView(0).findViewById<AppCompatTextView>(R.id.tvReposNum).text = "${it.repoNum} Repositories"
                            Glide.with(this@MainActivity)
                                .load(it.avatarUrl)
                                .placeholder(R.mipmap.ic_launcher)
                                .into(navMain.getHeaderView(0).findViewById(R.id.ivAvatar))
                        } ?: kotlin.run {
                            // maybe sign out to do
                        }
                })
            fetchCurrUser()
        }
        setupView()
    }

    private fun setupView() {
        setSupportActionBar(dToolbar)
        supportActionBar?.apply {
                setDisplayHomeAsUpEnabled(true)
                setDisplayShowHomeEnabled(true)
        }
        navController = Navigation.findNavController(this, R.id.navHost)
        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        NavigationUI.setupWithNavController(navMain, navController)
        navMain.menu.findItem(R.id.menu_repos).isCheckable = true
        navMain.setNavigationItemSelectedListener {
            if (it.itemId != R.id.menu_settings) it.isChecked = true
            when (it.itemId){
                R.id.menu_repos -> {
                    launchViewModel.selectModule(LauncherViewModel.HomeModule.REPOSITORIES)
                }
                R.id.menu_stars -> {
                    launchViewModel.selectModule(LauncherViewModel.HomeModule.STARS)
                }
                R.id.menu_settings -> {

                }
            }
            drawerLayout.closeDrawers()
            return@setNavigationItemSelectedListener true
        }
        navMain.getHeaderView(0).findViewById<AppCompatImageView>(R.id.ivAvatar)
            .setOnClickListener {
                drawerLayout.closeDrawer(GravityCompat.START)
                launchViewModel.userInfo.value?.run {
                    Toast.makeText(this@MainActivity, "TO USER PROFILE", Toast.LENGTH_SHORT).show()
                } ?: kotlin.run {
                    navController.navigate(R.id.action_to_sign_in)
                }
            }
        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id == R.id.mainPage){
                launchViewModel.moduleFlag.value?.let {
                    navMain.menu.findItem(
                        when (it){
                            LauncherViewModel.HomeModule.REPOSITORIES -> R.id.menu_repos
                            else -> R.id.menu_stars
                        }
                    ).isChecked = true
                }
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            }
        }
    }

    override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }
}
